<?php

namespace App\Models\Common;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class EditLog extends Model
{
    protected $table = 'edit_log';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    public static function getList(Request $request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;
        $manger_id = $memberinfo['id'] ?? 0;

        $query = self::where(['cpid'=>$cpid])->orderBy('create_at', 'desc');


        $start_time = intval($request->start_time);
        $end_time = intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }
        /***
         * order_type 1采购订单，2采购进货单，3采购入库单，11销售订货单、12销货单、13出库单
         * editor_mid 修改人id
         *
         */
        $order_type = $request->order_type;

        //6采购 7销售
        $validTypes = [];
        if (isset($order_type)) {
            if ($utype == '7') {
                $validTypes = [11, 12, 13];
            } elseif ($utype == '6') {
                $validTypes = [1, 2, 3];
            } else {
                $validTypes = [1, 2, 3, 11, 12, 13];
            }

            if (in_array($order_type, $validTypes)) {
                $query->where('order_type', $order_type);
            }
        } else {
            if ($utype == '7') {
                $validTypes = [11, 12, 13];
            } elseif ($utype == '6') {
                $validTypes = [1, 2, 3];
            } else {
                $validTypes = [1, 2, 3, 11, 12, 13];
            }

            $query->whereIn('order_type', $validTypes);
        }

        $order_sn_code = $request->order_sn_code;
        if ($order_sn_code!='') {
            $query->where('order_sn_code', "like",'%'.$order_sn_code.'%');
        }

        /***
         * 销售只能看到自己的日志
         *
         *
        */
        if ($utype == '7') {
            $query->where('editor_mid', '=', $manger_id);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
}
